<?php
/**
 * Created by PhpStorm.
 * User: ysfl
 * Date: 2015/11/25
 * Time: 1:28
 * 商品所有方法全部在这里
 * 添加后可以在导航管理查看以及管理
 */

//名字空间
namespace app\admin\controllers;

use modules\admin\SupplierModule;
use modules\admin\InventoryModule;

class   Inventory extends Admin
{
    //首页方法
    function index()
    {
        //调用视图并发送数据给视图
        $this->display($this->data);
    }

    //添加商品方法
    function  addInventory()
    {

        //注册模块
        $ADMIN = new InventoryModule();

        //注册供应商模块
        $G = new SupplierModule();

        //查询供应商
        $this->data['cname'] = $G->querySupplier();

        //提交检测
        if ($this->is_post()) {

            //发送数据给InventoryModule模块里的addinventory函数处理并取返回值赋予add_i_e
            $this->data['add_i_e'] = $ADMIN->addinventory($_POST);

        }

        //调用视图并发送数据给视图
        $this->display($this->data);
    }

    //查看商品方法
    function  queryInventory()
    {
        //注册模块
        $ADMIN = new InventoryModule();

        //把queryinventory()返回的数组赋予data['add_q_e']
        $this->data['query'] = $ADMIN->queryinventory();

        //调用视图并发送数据给视图
        $this->display($this->data);
    }

    //进货方法
    function  addInventoryi()
    {

        //注册模块
        $ADMIN = new InventoryModule();

        //获取商品名字
        $this->data['iname'] = $ADMIN->queryinventory();

        //获取店铺名字
        $this->data['sname'] = $ADMIN->queryshops();

        //获取商品分类
        $this->data['shopsname'] = $ADMIN->Shops_Type();

        //获取操作记录
        $this->data['Inventory_add_Log'] = $ADMIN->Inventory_Log(
            array(
                'select' => '*',
                'table'  => 'xrk_inventory_i',
                'order'  => 'time desc',
                'limit'  => '20'
            )
        );

        //获取未付款金额
        $this->data['Inventory_not_payment'] = $ADMIN->Not_Payment();

        //注册供应商模块
        $G = new SupplierModule();

        //查询供应商
        $this->data['cname'] = $G->querySupplier();

        //提交检测
        if ($this->is_post()) {

            //发送数据给InventoryModule模块里的addinventory函数处理并取返回值赋予add_i_e
            $this->data['add_i_e'] = $ADMIN->addinventoryi($_POST);

        }

        //调用视图并发送数据给视图
        $this->display($this->data);
    }

    //进货AJAX商品二级联动查询
    function AJAXQueryItem()
    {
        //注册模块
        $ADMIN = new InventoryModule();

        if ($this -> is_post()){

            switch($_POST['addr']){
                case "总仓库":
                    $addr = "warehouse";
                    break;
                case "白露店":
                    $addr = "bl";
                    break;
                case "文化广场店":
                    $addr = "wh";
                    break;
            }

            $type = $ADMIN->Q_Type(
                array(
                    'type' => $_POST['type'],
                    'addr' => $addr
                )
            );

            //是否排除库存为0的商品名字 1=排除 0=不排除
            $star = $_POST['star'];

            if ($star == 1){
                for ($i = 0; $type[$i] != ""; $i++){
                    if ($type[$i][$addr] != 0 || $type[$i]['type'] == "服务费"){
                        echo "<option>".$type[$i]['i_name']."</option>";
                    }
                }
            }
            else{
                for ($i = 0; $type[$i] != ""; $i++){
                    echo "<option>".$type[$i]['i_name']."</option>";
                }
            }
        }
    }

    //调货
    function Inventory_Transfer()
    {
        //注册模块
        $ADMIN = new InventoryModule();

        //获取店铺名字
        $this->data['sname'] = $ADMIN->queryshops();

        //获取商品分类
        $this->data['shopsname'] = $ADMIN->Shops_Type();

        //获取操作记录
        $this->data['Inventory_Transfer_Log'] = $ADMIN->Inventory_Log(
            array(
                'select' => '*',
                'table'  => 'xrk_inventory_transfer',
                'order'  => 'time desc',
                'limit'  => '20'
            )
        );

        if ($this->is_post()){

            $this->data['Inventory_Transfer_Info'] = $ADMIN->Inventory_Transfer($_POST);

        }

        //调用视图并发送数据给视图
        $this->display($this->data);
    }
}